import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.*;
import java.util.ArrayList;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegisterJFrame extends JFrame implements MouseListener {
    //定义变量
    JLabel accoutJLabel;
    JLabel passwordJLabel;
    JTextField accoutFiled;
    JTextField passwordFiled;
    JLabel loginJLabel;
    JLabel registerJLabel;
    JLabel verificationCodeJlabel;
    JTextField verificationCodeField;
    JLabel verificationCodeJlabel1;
    File file = new File("accout.txt");
    JLabel rl;
    String s = getCode();

    public RegisterJFrame() {
//设置位置
        accoutJLabel = new JLabel("账号：");//账号标签
        accoutJLabel.setFont(new Font("华文中宋", 1, 20));
        accoutJLabel.setBounds(80, 35, 100, 20);
        passwordJLabel = new JLabel("密码：");//密码标签
        passwordJLabel.setFont(new Font("华文中宋", 1, 20));
        passwordJLabel.setBounds(80, 90, 100, 20);
        accoutFiled = new JTextField();//账号文本
        accoutFiled.setBounds(145, 30, 300, 30);
        accoutFiled.setFont(new Font("华文中宋", 1, 20));
        passwordFiled = new JTextField();//密码文本
        passwordFiled.setBounds(145, 85, 300, 30);
        passwordFiled.setFont(new Font("华文中宋", 1, 20));
        loginJLabel = new JLabel(new ImageIcon("picture/登录按钮.png"));//登录注册按钮
        loginJLabel.setBounds(70, 175, 128, 47);
        registerJLabel = new JLabel(new ImageIcon("picture/注册按钮.png"));
        registerJLabel.setBounds(300, 175, 128, 47);


        verificationCodeJlabel = new JLabel("验证码：");
        verificationCodeJlabel.setFont(new Font("华文中宋", 1, 20));
        verificationCodeJlabel.setBounds(80, 135, 100, 20);
        verificationCodeField = new JTextField();//验证码文本
        verificationCodeField.setBounds(155, 130, 150, 30);
        verificationCodeField.setFont(new Font("华文中宋", 1, 20));
        verificationCodeJlabel1 = new JLabel(s);
        verificationCodeJlabel1.setFont(new Font("华文中宋", 1, 20));
        verificationCodeJlabel1.setBounds(310, 135, 100, 20);
        rl = new JLabel(new ImageIcon("picture//登录注册.png"));
        rl.setBounds(0, 0, 500, 300);

//添加到界面中

        this.add(verificationCodeJlabel1);
        this.add(verificationCodeField);
        this.add(passwordJLabel);
        this.add(accoutJLabel);
        this.add(accoutFiled);
        this.add(passwordFiled);
        this.add(loginJLabel);
        this.add(registerJLabel);
        this.add(verificationCodeJlabel);
        this.add(rl);
        this.setTitle("抽奖系统注册界面");
        this.setSize(500, 300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setLayout(null);
        this.setVisible(true);
//增加点击事件
        loginJLabel.addMouseListener(this);
        registerJLabel.addMouseListener(this);
    }

    @Override
    public void mouseClicked(MouseEvent e) {

        if (e.getSource() == registerJLabel) {
            //获取文本内容
            String accout = accoutFiled.getText();
            String password = passwordFiled.getText();
            String code = verificationCodeField.getText();
            //判断是否正确输入账号密码和验证码
            if (!codematch(accout)) {
                JOptionPane.showConfirmDialog(null, "请输入正确的账号，应该只包括字母和数字并且在6到12个之间", "提示", JOptionPane.UNDEFINED_CONDITION);
                s = getCode();
                verificationCodeJlabel1.setText(s);
            } else if (!codematch(password)) {
                JOptionPane.showConfirmDialog(null, "请输入正确的密码，应该只包括字母和数字并且在6到12个之间", "提示", JOptionPane.UNDEFINED_CONDITION);
                s = getCode();
                verificationCodeJlabel1.setText(s);
            } else if (!code.equals(s)) {
                JOptionPane.showConfirmDialog(null, "请输入正确的验证码", "提示", JOptionPane.UNDEFINED_CONDITION);
                s = getCode();
                verificationCodeJlabel1.setText(s);
            } else {
                try {
                    BufferedReader br = new BufferedReader(new FileReader(file));
                    String str;
                    while ((str = br.readLine()) != null) {
                        String[] s = str.split(",");//用逗号分割
                        //判断账号密码是否输入正确
                        if (s[0].equals(accout) ) {
                            JOptionPane.showConfirmDialog(null, "已有此账号", "提示", JOptionPane.UNDEFINED_CONDITION);
                            br.close();
                            break;
                        }

                    }
                    //判断是否找到，没找到 写入
                    if (str == null) {

                        BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
                        bw.write(accout);
                        bw.write(',');
                        bw.write(password);

                              //定义按钮上的文字
                        bw.newLine();
                        bw.close();
                        JOptionPane.showConfirmDialog(null, "注册成功", "提示", JOptionPane.UNDEFINED_CONDITION);
                    }
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            }

        } else if (e.getSource() == loginJLabel) {
            this.setVisible(false);
            new LoginJFrame();


        }


    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }

    //获取随机的的验证码
    public static String getCode() {
        //创建一个集合
        ArrayList<Character> list = new ArrayList<>();
        //添加字母 a - z  A - Z
        for (int i = 0; i < 26; i++) {
            list.add((char) ('a' + i));//a - z
            list.add((char) ('A' + i));//A - Z
        }

        //生成4个随机字母
        String result = "";
        Random r = new Random();
        for (int i = 0; i < 4; i++) {
            //获取随机索引
            int randomIndex = r.nextInt(list.size());
            char c = list.get(randomIndex);
            result = result + c;//拼接
        }


        //在后面拼接数字 0~9
        int number = r.nextInt(10);
        //把随机数字拼接到result的后面
        result = result + number;

        //把字符串变成字符数组
        char[] chars = result.toCharArray();//[A,B,C,D,5]
        //在字符数组中生成一个随机索引
        int index = r.nextInt(chars.length);
        //拿着4索引上的数字，跟随机索引上的数字进行交换
        char temp = chars[4];
        chars[4] = chars[index];
        chars[index] = temp;
        //把字符数组再变回字符串
        String code = new String(chars);

        return code;
    }

    //正则表达式判断
    public static boolean codematch(String s) {
        //随机的英文和数字6-12个
        String regex = "([a-zA-Z0-9]){6,12}";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(s);
        return matcher.find();
    }
}
